//
//  ICNotifyMsgCell.m
//  ICome
//
//  Created by ENN on 16/6/14.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICNotifyMsgCell.h"
#import "ICNotify.h"
#import "ICMessageNetworkManager.h"
#import "ICHeaderFile.h"

#define rightLabelW App_Frame_Width - (124+40+10)

@interface ICNotifyMsgCell ()

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *titleL;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIView *lineView1;


@property (nonatomic, strong) UIView * contentV;

@end

@implementation ICNotifyMsgCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.unreadPoint];
        
        [self.bgView addSubview:self.headImageView];
        [self.bgView addSubview:self.titleL];
        
        [self.bgView addSubview:self.contentLabel];
        [self.bgView addSubview:self.lineView];
        [self.bgView addSubview:self.lineView1];
        [self.bgView addSubview:self.contentV];
        
        [self setConstraint];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [self.contentView addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapAction {
    
    [self routerEventWithName:GXRouterEventNoticeMessage
                     userInfo:@{@"url":self.message.url == nil ? @"":self.message.url,
                                @"messageId":self.message.messageId,
                                @"currentCell":self}];
}


#pragma mark - Constraint
- (void)setConstraint
{
    [self.unreadPoint mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.centerY.equalTo(self.headImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.unreadPoint.mas_right).offset(12);
        make.right.equalTo(self.contentView.mas_right).offset(-38);
        make.top.equalTo(self.contentView.mas_top).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
    }];
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.top.equalTo(self.bgView.mas_top).offset(12);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(8);
        make.right.equalTo(self.bgView.mas_right).offset(-15);
        make.centerY.equalTo(self.bgView.mas_top).offset(25);
//        make.top.equalTo(self.bgView.mas_top).offset(5);
        make.height.mas_equalTo(23);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-12);
        make.top.equalTo(self.bgView.mas_top).offset(50);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-12);
        make.top.equalTo(self.lineView.mas_top).offset(10);//12
    }];
    
    [self.lineView1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-12);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(12);
        make.right.equalTo(self.bgView.mas_right).offset(-12);
        make.top.equalTo(self.lineView1.mas_bottom).offset(10);
        make.width.mas_equalTo(App_Frame_Width-60);
        make.height.mas_equalTo(11);
    }];
}

- (void)setMessage:(ICNotify *)message
{
    _message = message;
    if(_message){
        
        
        
        if(self.message.summary.length > 0){
            self.contentLabel.hidden = NO;
            self.contentLabel.text = message.summary; //[message.dictkey objectForKey:@"subject"];
//            [self.contentLabel sizeToFit];
            // 强制更新
//            [self layoutIfNeeded];
//            CGSize subjectSize = [self.message.summary sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width - 85 andFont:[UIFont systemFontOfSize:16]];
            CGFloat subjectHeight = [ICNotifyMsgCell contentLabelheight:self.message.summary];
            [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(subjectHeight);
            }];
        }else{
            self.contentLabel.hidden = YES;
            [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
        }
        
        if (message.showIcon == 1) {
            self.headImageView.hidden = YES;
            [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.bgView.mas_left);
                make.top.equalTo(self.bgView.mas_top);
                make.size.mas_equalTo(CGSizeMake(0, 0));
            }];
            self.titleL.text = @"";
            self.titleL.hidden = YES;
            [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(0);
            }];
            self.lineView.hidden = YES;
            [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.bgView.mas_top);
            }];
            self.lineView1.hidden = NO;
            [self.lineView1 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLabel.mas_bottom).offset(5);
            }];
            
        } else {
            self.headImageView.hidden = NO;
            [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.bgView.mas_left).offset(12);
                make.top.equalTo(self.bgView.mas_top).offset(12);
                make.size.mas_equalTo(CGSizeMake(30, 30));
            }];
            
            self.titleL.hidden = NO;
            [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(23);
            }];
            self.lineView.hidden = NO;
            [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.bgView.mas_top).offset(50);
            }];
            
            self.lineView1.hidden = YES;
            [self.lineView1 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentLabel.mas_bottom);
            }];
            
            WEAKSELF
            [ICMessageDatabase getBaseInfoWithgId:message.fr finish:^(NSArray *array, NSError *error) {
                if (array.count > 0) {
                    ICUser *info = [array firstObject];
                    weakSelf.titleL.text = info.eName;
                    [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(info.photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
                } else {
                    [[ICMessageNetworkManager sharedInstance] getFromInfoWithParam:@{@"id":message.fr} InfoCache:^(id responseCache) {
                        NSString *name = [[responseCache objectForKey:@"data"] objectForKey:@"name"];
                        NSString *photoId = [[responseCache objectForKey:@"data"] objectForKey:@"photoId"];
                        weakSelf.titleL.text =  name;
                        [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
                    } success:^(id object) {
                        NSString *name = [[object objectForKey:@"data"] objectForKey:@"name"];
                        NSString *photoId = [[object objectForKey:@"data"] objectForKey:@"photoId"];
                        weakSelf.titleL.text =  name;
                        [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getMaxImageURL:(photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"App_personal_headimg"]];
                    } failure:nil];
                }
            }];
        }
        
        if (_message.status == ICMessageStatus_unRead) {
            self.unreadPoint.hidden = NO;
        } else {
            self.unreadPoint.hidden = YES;
        }
        
        // 强制更新
//        [self layoutIfNeeded];
        [self.contentV removeAllSubviews];
        CGFloat messAgeH = 0;
        for(NSInteger i = 0;i < message.content.count;i++){
            NSDictionary * dict = message.content[i];
            CGFloat Hight = [ICNotifyMsgCell calculateheight:dict[@"value"]];
            [self addView:dict  count:i  height:Hight topheight:messAgeH];
            if(Hight > 22){
                messAgeH += (Hight+7);
            }else{
                messAgeH += 22;
            }
        }
        
        [self.contentV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(messAgeH + 11);
        }];
        
        // 强制更新
        [self layoutIfNeeded];
    }
}

-(void)addView:(NSDictionary *)dict  count:(NSUInteger)count  height:(CGFloat)height topheight:(CGFloat)topheight
{
    UILabel * leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, topheight, 60,15)];
    leftLabel.textColor = ICRGB(0x999999);
    leftLabel.font = [UIFont systemFontOfSize:14];
    leftLabel.textAlignment = NSTextAlignmentLeft;
    NSString * str = [dict objectForKey:@"key"];
//    leftLabel.text = [NSString stringWithFormat:@"%@",str.length > 4 ?[str substringFromIndex:str.length-4]:str];
    leftLabel.text = str;
    
    UILabel * centerLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftLabel.right, topheight, 5,15)];
    centerLabel.textColor = ICRGB(0x999999);
    centerLabel.font = [UIFont systemFontOfSize:14];
    centerLabel.textAlignment = NSTextAlignmentRight;
    centerLabel.text = @"：";
    
    UILabel * rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(centerLabel.right + 10,topheight,rightLabelW,height)];
    rightLabel.textColor = ICRGB(0x333333);
    rightLabel.font = [UIFont systemFontOfSize:14];
    rightLabel.numberOfLines = 0;
    rightLabel.textAlignment = NSTextAlignmentLeft;
    rightLabel.text = [dict objectForKey:@"value"];
    
    [self.contentV addSubview:leftLabel];
    [self.contentV addSubview:centerLabel];
    [self.contentV addSubview:rightLabel];
    
}


+(CGFloat)calculateheight:(NSString *)str
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, rightLabelW, 15)];
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.text = str;
    [label sizeToFit];
    return label.frame.size.height;
}

+(CGFloat)contentLabelheight:(NSString *)str
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width - 60, 15)];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.text = str;
    [label sizeToFit];
    return label.frame.size.height;
}

#pragma mark - Getter


- (UIImageView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIImageView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius  = 5;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = ICRGB(0xd9d9d9).CGColor;
        _bgView.layer.borderWidth = 0.5;
    }
    return _bgView;
}


-(UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.backgroundColor = [UIColor whiteColor];
    }
    return _headImageView;
}

- (UILabel *)titleL
{
    if (!_titleL) {
        _titleL        = [[UILabel alloc] init];
        _titleL.textColor = ICTEXTCOLOR_T1;
        _titleL.font = [UIFont systemFontOfSize:16];
        
    }
    return _titleL;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = ICRGB(0x333333);
        _contentLabel.font = [UIFont systemFontOfSize:16];
    }
    return _contentLabel;
}


-(UIView *)lineView
{
    if(!_lineView)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = ICRGB(0xd9d9d9);
    }
    return _lineView;
}

-(UIView *)lineView1
{
    if(!_lineView1)
    {
        _lineView1 = [[UIView alloc] init];
        _lineView1.backgroundColor = ICRGB(0xd9d9d9);
        _lineView1.hidden = YES;
    }
    return _lineView1;
}

-(UIView *)contentV
{
    if(!_contentV)
    {
        _contentV = [[UIView alloc] init];
        _contentV.backgroundColor = [UIColor clearColor];
    }
    return _contentV;
}

-(UILabel *)unreadPoint {
    if (!_unreadPoint) {
        _unreadPoint = [[UILabel alloc] init];
        _unreadPoint.backgroundColor = [UIColor redColor];
        _unreadPoint.layer.masksToBounds = YES;
        _unreadPoint.layer.cornerRadius = 6;
        [self.contentView addSubview:_unreadPoint];
    }
    return _unreadPoint;
}

@end
